﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure;
using B.S.BaseData.Write.API.Application.Command;
using MediatR;

namespace B.S.BaseData.Write.API.Application.CommandHnadler
{
    /// <summary>
    /// 养殖信息创建命令
    /// </summary>
    public class BreedingCreateCommandHandler : IRequestHandler<BreedingCreateCommand,ApiResult<object>>
    {
        /// <summary>
        /// 养殖信息仓储
        /// </summary>
        private readonly IBaseRepository<BreedingInfo> breedRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;


        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="breedRepository">养殖信息仓储</param>
        /// <param name="mapper">映射</param>
        public BreedingCreateCommandHandler(IBaseRepository<BreedingInfo> breedRepository,IMapper mapper)
        {
            this.breedRepository = breedRepository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        public Task<ApiResult<object>> Handle(BreedingCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            result.Code = ApiEnums.添加成功;
            result.Msg = ApiEnums.添加成功.ToString();
            var use = mapper.Map<BreedingInfo>(request);
            result.Data = breedRepository.Add(use);
            return Task.FromResult(result);
        }
    }
}
